package com.example.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.entity.Student;
import com.example.entity.Students;
import com.example.service.StudentInterface;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 卓小建
 * Date: 2018-12-18
 * Time: 13:50
 * rest Service
 */
@Path("/")
public class StudentInterfaceImpl implements StudentInterface {
    @Autowired
    private StudentService studentService;
    @Override
    @GET
    @Path("/getjson/{id:[0-9]{0,10}}")
    @Produces({MediaType.APPLICATION_JSON})
    public Student getStudent(@PathParam("id") Integer id) {
        return studentService.getStudent(id);
    }

    @GET
    @Path("/getxml/{id}")
    @Produces({MediaType.APPLICATION_XML})
    public Student getStudent2(@PathParam("id") Integer id) {
        return studentService.getStudent(id);
    }

    @Override
    @GET
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Path("/getmany/{ids}")
    public Students getAllStudent(@PathParam("ids") String ids) {
        Students students = new Students(new ArrayList<Student>());
        JSONObject jsonObject = JSONObject.parseObject(ids);
        JSONArray jsonArray = jsonObject.getJSONArray("id");
        for(int i = 0; i < jsonArray.size(); i++){
            Integer s = jsonArray.getInteger(i);
            if(s != null){
                students.getStudents().add(studentService.getStudent(s));
            }else{
                continue;
            }

        }
        return students;
    }
}
